package com.shadow.bus.main.complete;

import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.PersonEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/10
 */

public class CompleteAdapter extends MultipleRecyclerAdapter {
    protected CompleteAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT,
                R.layout.item_trouble_complete_content);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH,
                R.layout.item_trouble_complete_punch);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK,
                R.layout.item_trouble_complete_rework);
    }

    @Override
    protected void convert(MultipleViewHolder helper, MultipleItemEntity item) {
        switch (item.getItemType()) {
            case ItemType.TYPE_ITEM_TROUBLE_CONTENT:
                TroubleEntity troubleEntity = item.getField(ItemFiled.ITEM_OBJ);
                if (troubleEntity != null) {
                    String name = troubleEntity.getName();
                    if (name != null && !name.isEmpty()) {
                        helper.setText(R.id.tv_name, name.replaceAll("自定义录入@@", "附加作业项"));
                    }
                }
                helper.addOnClickListener(R.id.btn_rework);
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK:
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH:
                TroubleOperationEntity operationRework = item.getField(ItemFiled.ITEM_OBJ);
                if (operationRework != null) {
                    String name = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                    if (name != null && !name.isEmpty()) {
                        helper.setText(R.id.tv_trouble_des, name.replaceAll("自定义录入@@", "附加作业项"));
                    }
                    if (operationRework.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationRework.getOperation().getName());
                        PersonEntity personRework = operationRework.getWorkPerson();
                        helper.setText(R.id.tv_person, personRework == null ? "无打卡人" : personRework.getName());
                    }
                    PartEntity partRework = operationRework.getAccessory() == null ? operationRework.getComponent() : operationRework.getAccessory();
                    if (partRework != null) {
                        helper.setText(R.id.tv_part, partRework.getName());
                        helper.setText(R.id.tv_count, operationRework.getUseCount() + "");
                        helper.setText(R.id.tv_unit, partRework.getUnit());
                    }
                }
                break;
        }
    }
}
